package org.mm.service.impl;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.mm.model.User;
import org.mm.repository.UserRepository;
import org.mm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

/**
 * UserServiceImpl
 *
 * @author Marco Traversari
 */
@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserRepository userRepository;

	@Transactional(readOnly = true)
	public User findByUserNameAndUserPassword(String userName, String userPassword) throws Exception  {
		String hashedPassword = DigestUtils.sha1Hex(userPassword);
		return userRepository.findByUserNameAndUserPassword(userName, hashedPassword);
	}

	@Transactional(readOnly = true)
	public User findByUserNameAnd(String userName) throws Exception  {
		return userRepository.findByUserName(userName);
	}

	@Transactional(readOnly = true)
	public List<User> findAll() throws Exception {
		return Lists.newArrayList(userRepository.findAll(new Sort(Sort.Direction.ASC, "userId")));
	}
	
	@Transactional(readOnly = true)
	public User findByUserName(String userName) throws Exception {
		return userRepository.findByUserName(userName);
	}

	@Override
	public User findById(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User save(User user) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<User> findAllByPage(Pageable pageable) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(User user) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Page<User> search(User user) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}


}
